﻿using Microsoft.SharePoint;
using System;
using System.Linq;
using System.Collections.Generic;
using VPLink2013.Framework;
using VPLink2014.Data;
using VPLink2014.Data.Notifications;

namespace VPLink2014
{
    /// <summary>
    /// List Item Events
    /// </summary>
    public class NotificationsEspaces : SPItemEventReceiver
    {
        private static void NotifyUsers(SPItemEventProperties properties)
        {
            ContextTrace.AddTrace(string.Format("Notifier les users "));
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(properties.SiteId))
                {
                    using (SPWeb web = site.OpenWeb(properties.RelativeWebUrl))
                    {
                        ContextTrace.AddTrace(string.Format("Site trouvé"));
                        SPList currentList = web.Lists[properties.ListId];
                        //Get all users from List permissions 
                        List<SPUser> usersToNotify = web.SiteUsers.Cast<SPUser>()
                                                    .Where(x => currentList.DoesUserHavePermissions(x, SPBasePermissions.ViewListItems)).ToList<SPUser>();
                        ContextTrace.AddTrace(string.Format("nombre de personnes sur la liste : {0} - {1}", properties.ListTitle, usersToNotify.Count));
                        //Update each user's notification properties
                        using (VPLinkLogEntities db = new VPLinkLogEntities())
                        {
                            usersToNotify.ForEach(user => db.SaveUpdatedItemToNotifications(user.LoginName, web.Title,
                                properties.ListTitle,
                                properties.ListId.ToString(),
                                properties.ListItem.Name,
                                properties.ListItemId.ToString()));
                        }
                    }
                }
            });
        }
        /// <summary>
        /// An item is being added.
        /// </summary>
        public override void ItemAdding(SPItemEventProperties properties)
        {
            try
            {
                base.ItemAdding(properties);
                NotifyUsers(properties);
            }
            catch (Exception ex)
            {
                ContextTrace.AddTrace(ex.Message);
                ContextTrace.AddTrace(ex.StackTrace);
            }
            ContextTrace.Save(@"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\TEMPLATE\LAYOUTS\VPLink2014\Notifications", "trace");
        }

        /// <summary>
        /// An item is being updated.
        /// </summary>
        public override void ItemUpdating(SPItemEventProperties properties)
        {
            ContextTrace.AddTrace("Mise à jour item");
            try
            {
                base.ItemUpdating(properties);
                NotifyUsers(properties);
            }
            catch (Exception ex)
            {
                ContextTrace.AddTrace(ex.Message);
                ContextTrace.AddTrace(ex.StackTrace);
            }
            ContextTrace.Save(@"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\TEMPLATE\LAYOUTS\VPLink2014\Notifications\Trace", "trace");
        }

        /// <summary>
        /// An item is being checked in.
        /// </summary>
        public override void ItemCheckingIn(SPItemEventProperties properties)
        {
            try
            {
                base.ItemCheckingIn(properties);
                NotifyUsers(properties);
            }
            catch (Exception ex)
            {
                ContextTrace.AddTrace(ex.Message);
                ContextTrace.AddTrace(ex.StackTrace);
            }
            ContextTrace.Save(@"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\LAYOUTS\VPLink2014\Notifications", "notifs_trace");
        }
    }
}